import React from 'react';
import { Router, Route, IndexRoute } from 'dva/router';
import Login from './routes/login/Index.js';
import Pwd from './routes/login/Pwd';
import App from './routes/App.js';
import ErrPage from './routes/Error';
import User from './routes/user';
import { isLogin } from './utils/login.js';
import { message } from 'antd';
import Home from './routes/home/index.js';
import Banner from './routes/Banner';
import BillInforM from './routes/BillInforM';
import CustomerInformation from './routes/CustomerInformation';
import CustomerInforDetail from './routes/CustomerInforDetail';
import Price from './routes/price';
import BillUphold from './routes/billUphold';
import UserTwo from './routes/userTwo';
import CustomerInformationR from './routes/CustomerInformationR';

function redirectToLogin(nextState, replace) {
  if (!isLogin()) {
    message.warning('您尚未登录');
    location=`${location.origin}/login`;
  }
}

function redirectToDashboard(nextState, replace) {
  console.log(isLogin());
  if (isLogin()) {
      message.success('当前已登录');
    replace('/home')
  }
}

function RouterConfig({ history }) {

  return (
    <Router history={history}>
      <Route path="/" component={App} onEnter={redirectToLogin}>
        <IndexRoute component={Home}/>
        <Route path="user" component={User} />
        <Route path="customerInformation" component={CustomerInformation} />
        <Route path="customerInforDetail" component={CustomerInforDetail} />
        <Route path="BillInforM" component={BillInforM} />
        <Route path="price" component={Price} />
        <Route path="billUphold" component={BillUphold} />
        <Route path="userTwo" component={UserTwo} />
        <Route path="customerInformationR" component={CustomerInformationR} />
        <Route path="home" component={Home} />
        <Route path="echarts" component={Banner}/>
        <Route path="repwd" component={Pwd}/>

      </Route>
      <Route path="/login" component={Login} onEnter={redirectToDashboard}/>
      <Route path="/repwd" component={Pwd} onEnter={redirectToDashboard}/>
      <Route path="*" component={ErrPage}/>
    </Router>
  );
}

export default RouterConfig;
